<?php

/**
 * @package Basic Analytics
 * @version .1
 */
/*
Plugin Name: Basic Analytics
Plugin URI: http://benjamin-smith.com/code
Description: Basic plugin to allow tracking of your wordpress site by google analytics.
Author: Benjamin Smith
Version: .1
Author URI: http://benjamin-smith.com
License: GPL
*/
/*  
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

register_activation_hook( __FILE__, 'bss_basic_analytics_activate' );

register_deactivation_hook( __FILE__, 'bss_basic_analytics_deactivate' );

add_action( 'admin_menu', 'bss_basic_analytics_create_menu' );

add_action( 'wp_head', 'bss_basic_analytics' );


// Activation  function
function bss_basic_analytics_activate() {
	add_option( 'bss_basic_analytics_web_property_id', 'UA-0000000-0' );
	}		
	
// Deactivation function
function bss_basic_analytics_deactivate() {
	delete_option( 'bss_basic_analytics_web_property_id' );
	}
	
function bss_basic_analytics_create_menu() {
	//Create settings page to Dashboard Settings menu
	add_options_page( 'Basic Analytics Settings Page', 'Basic Analytics', 'administrator', __FILE__, 'bss_basic_analytics_settings_page');
		
	//call register settings function
	add_action( 'admin_init', 'bss_basic_analytics_register_settings' );
}

function bss_basic_analytics_register_settings() {
	register_setting( 'basic-analytics-group', 'bss_basic_analytics_web_property_id' );
		//register the settings
}

function bss_basic_analytics_settings_page() { ?>
	<div class="wrap">
	<h2><?php _e( 'Basic Analytics Plugin Options', 'basic-anayltics-plugin' ); ?> </h2>
	<form method="post" action="options.php">
	<?php settings_fields( 'basic-analytics-group' ); ?>
	<table class="form-table">
	<tr valign="top">
	<th scope="row"><?php _e('Web Property Id', 'basic-anayltics-plugin' ); ?> </th>
	<td><input type="text" name="bss_basic_analytics_web_property_id" value="<?php echo get_option( 'bss_basic_analytics_web_property_id'); ?>" />
	</td></tr>
	</table>
	<p class="submit">
	<input type="submit" class="button-primary" value="<?php _e('Save Changes', 'basic-analytics-plugin'); ?>" />
	</p></form></div>
<?php } 	
// Function to insert google anayltics javascript into theme pages
function bss_basic_analytics() {
$web_property_id = get_option( 'bss_basic_analytics_web_property_id' );
?>
<script type="text/javascript">
	var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '<?php echo $web_property_id; ?>']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
  </script>
<?php }	
?>